package sk.mka.phd.timit;

import java.io.File;
import java.util.List;
import org.apache.log4j.Logger;
import sk.mka.phd.files.reader.IFileListing;
import sk.mka.phd.timit.data.LoadFileOutputDataImpl;
import sk.mka.phd.timit.storage.IPhonemeStorage;
import sk.mka.phd.timit.storage.PhonemeStorage;
import sk.mka.utils.files.filters.FilteredFileListing;
import sk.mka.utils.files.reader.load.square.AbstractSquareDataLoadFile;

/**
 *
 * @author bracek
 * @date Jul 20, 2010
 */
public final class GetFilesImpl implements IGetFiles {

    private String inputFolder;
    private String outputFolder;
    private final IFileListing fileListing = new FilteredFileListing("_out.csv", "csv");
    private final static Logger log = Logger.getLogger(AbstractSquareDataLoadFile.class);
    private LoadFileOutputDataImpl loadFile;
    private IPhonemeStorage phonemeStorageSingleton;
    private boolean init = false;

    public GetFilesImpl(String inputFolder, String outputFolder) {
        this.inputFolder = inputFolder;
        this.outputFolder = outputFolder;
    }

    @Override
    public void getFiles() {

        //phonemeStorageSingleton = new PhonemeStorage(loadFile.getColsMaxLength(), this.outputFolder);
        final List<File> files = fileListing.getFiles(this.inputFolder);
        for (File file : files) {
            log.debug(file);
            loadFile = new LoadFileOutputDataImpl(file.getAbsolutePath(), ",");
            //phonemeStorageSingleton = PhonemeStorageSingleton.getInstance(loadFile.getColsMaxLength(), this.outputFolder);
            phonemeStorageSingleton = new PhonemeStorage();
            if (!init) {
                phonemeStorageSingleton.initialize(this.outputFolder, loadFile.getColsMaxLength());
                init = true;
            }

            loadFile.findWinner(phonemeStorageSingleton);
        }

        phonemeStorageSingleton.closeOutputFiles();
    }
}
